package org.pearl.springbootsecurity.demo.test;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.statement.values.ValuesStatement;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by TD on 2021/11/8
 */
public class SQLtest {
    public static void main(String[] args) throws JSQLParserException {

        List<SelectItem> list1 = new ArrayList<>();

        SelectExpressionItem selectExpressionItem = new SelectExpressionItem();
        selectExpressionItem.setExpression(new Column("s"));
        list1.add(selectExpressionItem);


        String SQL002 = "SELECT t1.a , t1.b  FROM tab1 AS t1 JOIN tab2 t2 ON t1.user_id  = t2.user_id";   // 多表SQL
        // WithItem
        String SQL003 = "WITH t AS ( SELECT * FROM user WHERE user.user_name = 'test' ) SELECT t.* FROM t";
        // PlainSelect
        String SQL004 = "SELECT * FROM user WHERE user.user_id in (SELECT dept_id FROM dept WHERE depe.id = '0001' )";
        String VALUES = "SELECT *\n" +
                "INTO Persons_backup\n" +
                "FROM Persons";
        // 1.
        Select select = (Select) CCJSqlParserUtil.parse(SQL002);
        PlainSelect selectBody1 =(PlainSelect) select.getSelectBody();
        selectBody1.setSelectItems(list1);



        Select parse = (Select) CCJSqlParserUtil.parse(SQL003);
        Select parse1 = (Select) CCJSqlParserUtil.parse(SQL004);
        Select values = (Select) CCJSqlParserUtil.parse(VALUES);

        SelectBody selectBody = parse.getSelectBody();
        List<WithItem> withItemsList = parse.getWithItemsList();


        ValuesStatement valuesStatement = new ValuesStatement();
        List<Expression> list = new ArrayList<>();
        EqualsTo equalsTo = new EqualsTo(); // 等于表达式
        equalsTo.setLeftExpression(new Column("user_id")); // 设置表达式左边值
        equalsTo.setRightExpression(new StringValue("123456"));//
        list.add(equalsTo);
        valuesStatement.setExpressions(list);

        // SetOperationList
        String aaa = "u.create_time > \"2021-01-01 00:00:00\"";
        Expression expression = CCJSqlParserUtil.parseCondExpression(aaa);


        //
        String sdsad = "values row(1,2,3)";
        Select sadsadsa = (Select) CCJSqlParserUtil.parse(sdsad);

        System.out.println("==================");
    }
}
